/* jStars, a star rating Plugin for jQuery
 * Author: David Gonzalez
 * License: LGPL
 * Version: 1.3
 * Release: 2010-02-01
 */ 
;(function($){
		   
$.fn.stars = function( arg1 , arg2 ){
	if(typeof arg1 === "string" ){
		var $this = $(this);
		if (arg1 == "val"){
			if(arg2 === undefined){
				return $this.data("val");
			}else{
				$this.data("val",arg2);
				$this.data("val_",arg2);
				draw($this);
			}
		}else if (arg1 == "rated"){
			if(arg2 === undefined){
				return $this.data("rated");
			}else{
				if(arg2 == true){
					if (!$this.data("rated")){
						$this.unbind("mouseenter");
						$this.data("rated",true);
					}
				}else{
					if ($this.data("rated")){
						$this.bind("mouseenter",$this, hoverHandler );
						$this.data("rated",false);
					}
				}
			}
		}else if(arg1 == "redraw"){
			var d = $this.data();
			$this.css({'cursor':'pointer','display':'inline-block','background':'url('+d.img+') repeat-x 0px 0px','height':d.imgh+'px'});
			d.bar.css({'background':'url('+d.img+') repeat-x 0px -'+d.imgh+'px','height':d.imgh+'px'});
			$this.width(d.stars*d.imgw);
			d.val_ = d.val;
			draw($this);
		}
	}else{
		var o = arg1;//var o = $.extend($.fn.stars.defaults, arg1 || {});
		var d = $.fn.stars.defaults;
		if( o == undefined ) var o = d;
		else{
			if( o.rated == undefined ) o.rated = d.rated;
			if( o.stars == undefined ) o.stars = d.stars;
			if( o.step == undefined ) o.step = d.step;
			if( o.val == undefined ) o.val= d.val;
			if( o.img == undefined ) o.img = d.img;
			if( o.imgw == undefined ) o.imgw= d.imgw;
			if( o.imgh == undefined ) o.imgh= o.imgw;
			if( o.onchange == undefined ) o.onchange = d.onchange;
			if( o.onrating == undefined ) o.onrating = d.onrating;
			if( o.onleave == undefined ) o.onleave = d.onleave;
		};
		this.each(function() {
			var $this = $(this);
			$this.css({'background':'url('+o.img+') repeat-x 0px 0px','height':o.imgh+'px'});
			o.val_ = o.val;
			o.bar = $("<div>",{css:{'background':'url('+o.img+') repeat-x 0px -'+o.imgh+'px','height':o.imgh+'px'}}).appendTo(this);
			$this.data(o);
			$this.width(o.stars*o.imgw);
			draw($this);
			if(!o.rated) $this.bind("mouseenter",$this, hoverHandler ).css('cursor','pointer');
		});
	};
	return this;
};

$.fn.stars.defaults = {
	rated:false,
	stars:5,
	step:1,
	val:0,
	img:"jStars.png",
	imgw:28,
	imgh:28,
	onchange: function(v){},
	onrating: function(v){}
};

function draw(el){
	var d = el.data();
	if(d.val>d.stars) d.val=d.stars;
	if(d.val<0) d.val=0;
	d.bar.width(d.val*d.imgw);
	d.onchange(d.val);
}

function hoverHandler(e){
	var d = e.data;
	d.data().bar.css('background-position','0 -'+d.data("imgh")*2+'px');
	d.bind('mouseleave', d, leaveHandler).one('click', d, clickHandler).bind('mousemove', d, moveHandler);
	moveHandler(e);
	return false;
}

function clickHandler(e){
	var d = e.data;
	d.data('val_',d.data('val'));
	d.unbind('mouseenter');
	leaveHandler(e);
	d.data().onrating(d.data('val_'));
	return false;
}

function moveHandler(e){
	var d = e.data.data();
	var t = ((e.clientX-e.data.offset().left)/d.imgw/d.step).toFixed(0)/(1/d.step);
	if (t != d.val){
		d.val = t;
		draw(e.data);
	}
}

function leaveHandler(e){
	var d = e.data;
	d.data().bar.css('background-position','0 -'+d.data("imgh")+'px');
	d.unbind('mousemove').unbind('mouseclick').unbind('mouseleave');
	d.data('val',d.data('val_'));
	draw(d);
	return false;
}

})( jQuery );
